<template>
  <div>
    <el-button
      type="primary"
      icon="el-icon-plus"
      class="buttons"
      @click="showDialog"
      >添加</el-button
    >
    <el-table style="width: 100%" border :data="list">
      <el-table-column label="序号" width="80px" align="center" type="index">
      </el-table-column>
      <el-table-column
        prop="tmName"
        label="品牌名称"
        width="width"
        align="center"
      >
      </el-table-column>
      <el-table-column label="品牌Logo" width="width" align="center">
        <template slot-scope="{ row, $index }">
          <img :src="row.logoUrl" style="width: 80px; height: 80px" />
        </template>
      </el-table-column>
      <el-table-column prop="prop" label="操作" width="width" align="center">
        <template slot-scope="{ row, $index }">
          <el-button
            type="warning"
            icon="el-icon-edit"
            size="mini"
            @click="updateTradeMark(row)"
            >修改</el-button
          >
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            @click="deleteTradeMark(row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      class="paginations"
      :current-page.sync="page"
      :page-sizes="[3, 5, 10]"
      :page-size="limit"
      layout="prev, pager, next, jumper, ->,sizes, total"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    >
    </el-pagination>
    <!--对话框 -->
    <el-dialog
      :title="tmform.id ? '修改品牌' : '添加品牌'"
      :visible.sync="dialogFormVisible"
    >
      <el-form style="width: 75%" :model="tmform" :rules="rules" ref="ruleform">
        <el-form-item
          label="品牌名称"
          :label-width="formLabelWidth"
          prop="tmName"
        >
          <el-input autocomplete="off" v-model="tmform.tmName"></el-input>
        </el-form-item>
        <el-form-item
          label="品牌Logo"
          :label-width="formLabelWidth"
          prop="logoUrl"
        >
          <el-upload
            class="avatar-uploader"
            action="/dev-api/admin/product/fileUpload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <!---->
            <img v-if="tmform.logoUrl" :src="tmform.logoUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            <div slot="tip" class="el-upload__tip">
              只能上传jpg/png文件，且不超过2M
            </div>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addOrUpdateTradeMark"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "tradeMark",
  data() {
    return {
      page: 1,
      limit: 3, //当前页数展示数据的条数
      total: 0,
      list: [], //列表展示的数据
      dialogFormVisible: false, //对话框显示与隐藏的属性
      formLabelWidth: "85px",
      tmform: {
        tmName: "",
        logoUrl: "",
      }, //手机品牌信息
      rules: {
        tmName: [
          { required: true, message: "请输入品牌名称", trigger: "blur" },
          { min: 2, max: 10, message: "长度在2到10个字符", trigger: "change" },
        ],
        logoUrl: [{ required: true, message: "请选择品牌Logo" }],
      }, //验证规则
    };
  },
  mounted() {
    //获取列表数据
    this.getPageList();
  },
  methods: {
    //获取列表数据
    async getPageList() {
      const { page, limit } = this;
      let result = await this.$API.tradeMark.reqTradeMarkList(page, limit);
      if (result.code == 200) {
        this.total = result.data.total;
        this.list = result.data.records;
      }
    },
    handleCurrentChange(page) {
      //修改参数
      this.page = page;
      this.getPageList();
    },
    handleSizeChange(limit) {
      //修改参数
      this.limit = limit;
      this.getPageList();
    },
    //点击添加按钮
    showDialog() {
      this.tmform = { tmName: "", logoUrl: "" };
      this.dialogFormVisible = true;
    },
    //点击修改按钮
    updateTradeMark(row) {
      this.dialogFormVisible = true;
      this.tmform = { ...row }; //将已有的品牌信息展示,并且浅拷贝
    },
    //上传图片相关
    handleAvatarSuccess(res, file) {
      this.tmform.logoUrl = res.data;
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isPNG = file.type === "image/png";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG && !isPNG) {
        this.$message.error("上传头像图片只能是 JPG/PNG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return (isPNG || isJPG) && isLt2M;
    },
    //添加或者修改确认按钮
    addOrUpdateTradeMark() {
      //当全部表单验证通过后再书写业务逻辑
      this.$refs["ruleform"].validate(async (valid) => {
        if (valid) {
          this.dialogFormVisible = false;
          let result = await this.$API.tradeMark.reqAddOrUpdateTradeMark(
            this.tmform
          );
          if (result.code == 200) {
            //弹出一个成功信息
            this.$message({
              message: this.tmform.id ? "修改成功" : "添加成功",
              type: "success",
            });
            this.getPageList();
          }
        } else {
          console.log("error submit");
          return false;
        }
      });
    },
    //删除按钮
    deleteTradeMark(row) {
      //弹框
      this.$confirm(`确认删除${row.tmName}？`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          let result = await this.$API.tradeMark.reqDeleteTradeMark(row.id);
          if (result.code == 200) {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            this.list.length > 1 ? "" : (this.page = this.page - 1);
            this.getPageList();
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>

<style>
.buttons {
  margin: 10px 0;
}
.paginations {
  margin: 20px 0 0 0;
  text-align: center;
}
.avatar-uploader .el-upload {
  border: 1px dashed #e4e4e4;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
